package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.pojo.Cj;
import com.service.impl.CjServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 类名称：CjController.java<br>
 * 类描述：基本上都是表单接受方式，后面的是ajax<br>
 * 创建时间：2017年3月20日, 上午10:08:03
 */
@Controller
@RequestMapping("/cj")
public class CjContrloller {
	Logger log=LogManager.getLogger(CjContrloller.class.getName());
	@Resource
	private CjServiceImpl cjServiceImpl;
	//	奖惩模块引导页面
	@RequestMapping("/cjLogin")
	private ModelAndView  LoginCjController(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("cj/cjLogin");
	}
	//	添加奖惩欢迎界面
	@RequestMapping("/addCj")
	private ModelAndView  addCjController(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("cj/addCj");
	}
	//	返回添加奖惩
	@RequestMapping(value = "/returnAddCj", method = RequestMethod.POST)
	private ModelAndView  returnAddCjController(HttpServletRequest request,HttpServletResponse response) {
		try{
			Cj cjDemo =new Cj();
			cjDemo.setCjTitle(request.getParameter("cjTitleForAdd"));
			cjDemo.setCjType(request.getParameter("cjTypeForAdd"));
			cjDemo.setCjContent(request.getParameter("cjContentForAdd"));
			cjDemo.setCjMoney(Integer.valueOf(request.getParameter("cjMoneyForAdd")));
			cjDemo.setCjTime(request.getParameter("cjTimeForAdd"));
			System.out.println(cjDemo.getCjContent());
			cjServiceImpl.addCj(cjDemo);
			return new ModelAndView("train/successful");
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("cj/addCj");
		}

	}
	//	删除奖惩
	@RequestMapping("/deleteCj/{id}")
	private ModelAndView  deleteCjController(HttpServletRequest request,HttpServletResponse response,@PathVariable("id")Integer id) {
		try{
			cjServiceImpl.deleteCj(id);
			return new ModelAndView("train/deleteTrain");
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error");
		}

	}
	//	查看奖惩
	@RequestMapping("/selectAllCj")
	private ModelAndView  seleteAllCjController(HttpServletRequest request,HttpServletResponse response) {
		List<Cj> returnSeleteAllCj =cjServiceImpl.selectAllCj();
		return new ModelAndView("cj/selectAllCj","returnSeleteAllCj",returnSeleteAllCj);
	}
	//	详查奖惩
	@RequestMapping("/selectByIdCj/{id}")
	private ModelAndView selectByIdCjController(HttpServletRequest request,HttpServletResponse response,@PathVariable("id")Integer id) {
		try{
			Cj cjDemo=cjServiceImpl.selectByIdCj(id);
			return new ModelAndView("cj/selectByIdCj","returnSelectByIdCj",cjDemo);
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error");
		}
	}
	/**
	 * ajax ajios 使用
	 * 无参数接口
	 * @param resp
	 */
	@RequestMapping("/getConnFileList")
	public void getConnFileList( HttpServletResponse resp){
		log.info("url getConnFileList");
		PrintWriter out;
		JSONObject ConnFileList = new JSONObject();
		try{
			List<Cj> returnSeleteAllCj =cjServiceImpl.selectAllCj();
			JSONArray connFileNameJson = JSONArray.fromObject(returnSeleteAllCj);
			ConnFileList.put("connFileNameList", connFileNameJson);
			ConnFileList.put("status", "0");
		}catch(Exception e){
			ConnFileList.put("status", "1");	// null
		}
		try {
			out = resp.getWriter();
			out.print(ConnFileList);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	/**
	 * ajax ajios 使用
	 * 有参数接口
	 * @param req
	 * @param resp
	 */
	@RequestMapping("/MDSOPenConn")
	private void   MDSOPenConn(HttpServletRequest req, HttpServletResponse resp) {
		log.info("url MDSOPenConn");
		PrintWriter out;
		JSONObject tables = new JSONObject();
		try{
			String connName = req.getParameter("connName");
//
//			TConnFilePath  tConnFilePath = connFileServiceImpl.selectConnFileByConnName(connName);
//			String filePath =tConnFilePath.getFilePath();
//
//			ArrayList<String> tableArr = jdbcConn.showTablesByEntrepot(filePath);
//
//			JSONArray tablejson = JSONArray.fromObject(tableArr);
			tables.put("status", "0");
//			tables.put("tables", tablejson);
//			tables.put("filePath", filePath);
		}catch(Exception e){
			e.printStackTrace();
			tables.put("status", "1");
		}
		try {
			out= resp.getWriter();
			out.print(tables);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
